﻿using System;
using System.Windows.Forms;
using EasyComm.Contracts;

namespace EasyComm.SDK.WinTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LogForm log = new LogForm();
            log.StartPosition = FormStartPosition.Manual;
            log.Location = new System.Drawing.Point(Screen.PrimaryScreen.WorkingArea.Width - log.Width, Screen.PrimaryScreen.WorkingArea.Height - log.Height);
            log.Show();
            this.Activate();
            EasyCommContext.Admin.NLBAdminServiceChange += new AdminServiceEventHandler<IAdminNLBContract>(Admin_NLBAdminServiceChange);
        }


        private void btnCheckNLBStatus_Click(object sender, EventArgs e)
        {
            //NLBStatusLabel.Text = "Checking NLB status...";
            //Application.DoEvents();
            //if (EasyCommContext.Router.Status == ServiceStatus.Online)
            //{
            //    NLBStatusLabel.Text = "Status: Online";
            //    lblRouterUrl.Text = EasyCommContext.Router.Contracts[0].ListenUri.ToString();
            //}
            //else
            //{
            //    NLBStatusLabel.Text = "Status: Offline";
            //    lblRouterUrl.Text = "offline";
            //}

            //if (EasyCommContext.Proxy.Status == ServiceStatus.Online)
            //{
            //    lblProxyUrl.Text = EasyCommContext.Proxy.Contracts[0].ListenUri.ToString();
            //}
            //else
            //{
            //    lblProxyUrl.Text = "offline";
            //}
        }

        private void btnCheckAdminStatus_Click(object sender, EventArgs e)
        {
            //lblAdminUrl.Text = "Checking status...";
            //Application.DoEvents();
            //if (EasyCommContext.Admin.Url != null)
            //{
            //    lblAdminUrl.Text = EasyCommContext.Admin.Url.ToString();
            //    //EasyCommContext.Admin.Initialize();
            //    listBox1.Items.Clear();
            //    if (EasyCommContext.Admin.Contracts.Count > 0)
            //    {
            //        EasyCommContext.Admin.Contracts.ForEach(contract => listBox1.Items.Add(contract.AssemblyPath));
            //    }
            //}
            //else
            //{
            //    lblAdminUrl.Text = "Status: offline";
            //}
        }

        private void btnAddContractAssembly_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (string fileName in openFileDialog1.FileNames)
                {
                    listBox1.Items.Add(fileName);
                    EasyCommContext.Admin.LoadWorkerServiceAssembly(fileName);
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //AdminServiceUnitCollection<IAdminSLBContract> slbList = EasyCommContext.Admin.SystemLoadBalancerList;
            //serviceTree.Nodes.Clear();
            //foreach (AdminServiceUnit<IAdminSLBContract> slbUnit in slbList)
            //{
            //    //slbUnit.Service.Proxy.
            //    serviceTree.Nodes.Add(string.Format("{0}:{1}", slbUnit.Host, slbUnit.Port), string.Format("[SLB] {0}:{1}", slbUnit.Host, slbUnit.Port));
            //}
            //WorkerServiceUnitCollection workerList = EasyCommContext.Admin.WorkerList;
            //foreach (WorkerServiceUnit workerUnit in workerList)
            //{
            //    TreeNode slbNode = GetSLBNode(serviceTree.Nodes, workerUnit.Host, workerUnit.Port);
            //    if (slbNode != null)
            //    {
            //        slbNode.Nodes.Add(string.Format("{0}:{1}", workerUnit.Host, workerUnit.Port), string.Format("[Worker] {0}:{1}", workerUnit.Host, workerUnit.Port));
            //    }
            //    else
            //    {
            //        serviceTree.Nodes.Add(string.Format("{0}:{1}", workerUnit.Host, workerUnit.Port), string.Format("[Worker] {0}:{1}", workerUnit.Host, workerUnit.Port));
            //    }
            //}
        }

        private TreeNode GetSLBNode(TreeNodeCollection nodeList, string host, int port)
        {
            string key = string.Format("{0}:{1}", host, port);
            foreach (TreeNode tn in nodeList)
            {
                if (tn.Name == key)
                {
                    return tn;
                }
                return GetSLBNode(tn.Nodes, host, port);
            }
            return null;
        }

        void Admin_NLBAdminServiceChange(AdminServiceEventArgs<IAdminNLBContract> e)
        {
            lblAdminUrl.Text = string.Format("[{0}] {1}", e.AvailabilityType, e.Unit.ListenUri);
            listBox1.Items.Clear();
            switch (e.AvailabilityType)
            {
                case ServiceAvailabilityType.Online:
                    //if (EasyCommContext.Admin.Contracts.Count > 0)
                    //{
                    //    EasyCommContext.Admin.Contracts.ForEach(contract => listBox1.Items.Add(contract.AssemblyPath));
                    //}
                    break;
                case ServiceAvailabilityType.Offline:
                    break;
                case ServiceAvailabilityType.Modified:
                    break;
                default:
                    break;
            }
            //throw new NotImplementedException();
        }
    }
}
